package com.app.web.stomp
import org.springframework.http.server.ServerHttpRequest
import org.springframework.http.server.ServerHttpResponse
import org.springframework.web.socket.WebSocketHandler
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor
/**
 * 链接握手拦截器
 * Created by three on 14-10-11.
 */
class MyHandshakeInterceptor extends HttpSessionHandshakeInterceptor{

    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {
        System.out.println("Before Handshake");
        /*if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            def p = request.getPrincipal()

            def p2 = servletRequest.getServletRequest().getUserPrincipal();
            attributes.put(SimpMessageHeaderAccessor.USER_HEADER, p2);

        }*/
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    @Override
    public void afterHandshake(ServerHttpRequest request,
                               ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception ex) {
        System.out.println("After Handshake");
        def method = request.method;
        response.getHeaders().add("Access-Control-Allow-Origin", "http://127.0.0.1:9000")
        response.getHeaders().add("Access-Control-Allow-Credentials", "true")
        response.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
        response.getHeaders().add("P3P","CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'")
        switch (method){
            case "OPTIONS" :

                response.getHeaders().add("Access-Control-Allow-Headers", "Cookie,Authorization,Range,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
                response.getHeaders().add("Access-Control-Expose-Headers", "Accept-Ranges,Content-Encoding,Content-Length,Content-Range")

                break;
            case 'POST':
            case "PUT":
            case "DELETE":
                response.getHeaders().add("Access-Control-Allow-Headers", "Cookie,Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
                break;
            case "GET":
                response.getHeaders().add("Access-Control-Allow-Headers", "Cookie,Authorization,Range,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type");
                response.getHeaders().add("Access-Control-Expose-Headers", "Accept-Ranges,Content-Encoding,Content-Length,Content-Range");
                break;
            case "HEAD":
                break;
        }
        super.afterHandshake(request, response, wsHandler, ex);
    }

}
